﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string ProductBrand()
        {
            string strWhere = "";//查询条件语句
            string mystyle = this.Label_Inserted;//标签显示内容
            string str_ShowType = this.GetParamValue("YX:ShowType");//0表示全部;1表示推荐
            string CompositorType = this.GetParamValue("YX:CompositorType");//排序
            string TempletLayout = this.GetParamValue("YX:TempletLayout");//布局方式
            string ShowMode = this.GetParamValue("YX:ShowMode");//显示方式
            string ShowRowsC = this.GetParamValue("YX:ShowRows");
            int ShowRows = 1;//显示行
            if (!string.IsNullOrEmpty(ShowRowsC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(ShowRowsC))
                {
                    ShowRows = Convert.ToInt32(ShowRowsC);//显示行
                }
            }
            string ShowColumnsC = this.GetParamValue("YX:ShowColumns");
            int ShoeColumns = 1;//显示列
            if (!string.IsNullOrEmpty(ShowColumnsC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(ShowColumnsC))
                {
                    ShoeColumns = Convert.ToInt32(ShowColumnsC);
                }
            }
            string NameCharacterC = this.GetParamValue("YX:NameCharacter");
            int NameCharacter = 20;//名字不能超过字符数
            if (!string.IsNullOrEmpty(NameCharacterC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(NameCharacterC))
                {
                    NameCharacter = Convert.ToInt32(NameCharacterC);//名字不能超过字符数
                }
            }
            string BewriteCharacterC = this.GetParamValue("YX:BewriteCharacter");
            int BewriteCharacter = 40;//描述不能超过字符数
            if (!string.IsNullOrEmpty(BewriteCharacterC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(BewriteCharacterC))
                {
                    BewriteCharacter = Convert.ToInt32(BewriteCharacterC);//描述不能超过字符数
                }
            }
            bool WhetherOmit = Convert.ToBoolean(this.GetParamValue("YX:WhetherOmit"));//是否省略
            bool WhetherPagination = Convert.ToBoolean(this.GetParamValue("YX:WhetherPagination"));//是否分页
            if (str_ShowType == "1")//显示类型
            {
                strWhere += " and isrecommend=1";
            }
            if (!string.IsNullOrEmpty(ShowMode))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(ShowMode))
                {
                    string CID = HttpContext.Current.Request.QueryString["q_cid"];
                    string ProID = HttpContext.Current.Request.QueryString["ProID"];
                    if (CID != null && CID != string.Empty)
                    {
                        CID = ChangeHope.Common.StringHelper.InputTexts(CID);
                        strWhere += " and cid='" + CID + "'";
                    }
                    if (ProID != null && ProID != string.Empty)
                    {
                        ProID = ChangeHope.Common.StringHelper.InputTexts(ProID);
                        YXShop.BLL.Product.ProductInfo bpd = new YXShop.BLL.Product.ProductInfo();
                        YXShop.Model.Product.ProductInfo dataPro = bpd.GetModel(Convert.ToInt32(ProID));
                        string ProCID = string.Empty;
                        if (dataPro != null)
                        {
                            ProCID = dataPro.pro_CID.ToString();
                        }
                        strWhere += " and cid='" + ProCID + "'";
                    }
                }
            }
            int i = 0, j = 0;
            int PageIndex = 1;
            string name = "PageIndex";//暂时以’PageIndex‘为跳转页面传的参数名,如果需要修改只需要修改这里的值就可以达到修改分页传参的作用
            if (HttpContext.Current.Request.QueryString[name] != null)
            {
                PageIndex = Convert.ToInt32(ChangeHope.Common.StringHelper.InputTexts(HttpContext.Current.Request.QueryString[name]));
                if (PageIndex < 1)
                {
                    PageIndex = 1;
                }
            }

            string OrderFields = string.Empty;
            if (CompositorType == "desc")//排序 降序
            {
                OrderFields = " [order by] sort desc";
            }
            else//升序
            {
                OrderFields = " [order by] sort asc";
            }
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(OrderFields, ShowRows * ShoeColumns, strWhere);
            StringBuilder shtml = new StringBuilder();
            int r = 0;
            if (!string.IsNullOrEmpty(mystyle))
            {
                string pattern = @"\{\#[\s\S]*?\#\}";
                string tempfinallyconent = mystyle;
                StringBuilder GetTemp = new StringBuilder();
                MatchCollection macths = Regex.Matches(tempfinallyconent, pattern);
                string Str_Tag = "";
                string temptag = "";
                foreach (Match macth in macths)
                {
                    temptag = macth.ToString();
                    int templength = temptag.ToString().IndexOf("{#") + 2;
                    int templength2 = temptag.ToString().IndexOf("#}");
                    if (temptag.ToString().Substring(templength, templength2 - templength).ToString() != null)
                    {
                        Str_Tag = temptag.ToString().Substring(templength, templength2 - templength);
                    }
                }
                StringBuilder content = new StringBuilder();
                if (dataPage.DataReader != null)
                {
                    YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                    while (dataPage.DataReader.Read())
                    {
                        string strContent = string.Empty;
                        if (Str_Tag != "")
                        {
                            strContent = Str_Tag;
                        }
                        else
                        {
                            strContent = mystyle;
                        }
                        if (strContent.IndexOf("[#Brand_Name]") > -1)
                        {
                            if (dataPage.DataReader["name"].ToString() != string.Empty && dataPage.DataReader["name"].ToString() != null)
                            {
                                if (WhetherOmit)
                                {
                                    strContent = strContent.Replace("[#Brand_Name]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["name"].ToString(), NameCharacter, "..."));
                                }
                                else
                                {
                                    strContent = strContent.Replace("[#Brand_Name]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["name"].ToString(), NameCharacter));
                                }

                            }
                            else
                            {
                                strContent = strContent.Replace("[#Brand_Name]", "");
                            }
                        }
                        if (strContent.IndexOf("[#Brand_Images]") > -1)
                        {
                            string imagesUrl = sp.DummyPaht + dataPage.DataReader["image"].ToString();
                            string images = dataPage.DataReader["image"].ToString();
                            if (!string.IsNullOrEmpty(images.Trim()))
                            {
                                if (images.Contains("http://") || images.Contains("HTTP://"))
                                {
                                    imagesUrl = dataPage.DataReader["image"].ToString();
                                }
                            }
                            if (!string.IsNullOrEmpty(imagesUrl.Trim()))
                            {
                                strContent = strContent.Replace("[#Brand_Images]", imagesUrl);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Brand_Images]", sp.DummyPaht + "images/NaughtImages.gif");
                            }
                        }
                        if (strContent.IndexOf("[#Brand_Description]") > -1)
                        {
                            if (dataPage.DataReader["description"].ToString() != string.Empty && dataPage.DataReader["description"].ToString() != null)
                            {
                                if (WhetherOmit)
                                {
                                    strContent = strContent.Replace("[#Brand_Description]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["description"].ToString(), BewriteCharacter, "..."));
                                }
                                else
                                {
                                    strContent = strContent.Replace("[#Brand_Description]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["description"].ToString(), BewriteCharacter));
                                }
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Brand_Description]", "");
                            }
                        }
                        if (strContent.IndexOf("{#BrandURL}") > -1)
                        {
                            strContent = strContent.Replace("{#BrandURL}", Common.UrlStr("productbrandlist", "q_bid=" + dataPage.DataReader["bid"].ToString() + "", "product/"));
                        }
                        content.Append(strContent);
                        if (TempletLayout != "0")
                        {
                            r++;
                            if (ShoeColumns % r == 0)
                            {
                                content.Append("</tr><tr>");
                            }
                        }
                    }

                    if (Str_Tag != "")
                    {
                        if (content.ToString() != string.Empty)
                        {
                            mystyle = mystyle.Replace(temptag, content.ToString());
                        }
                        else
                        {
                            mystyle = mystyle.Replace(temptag, "");
                        }
                        shtml.Append(mystyle);
                    }
                    else
                    {
                        shtml.Append(content.ToString());
                    }
                }
            }
            if (WhetherPagination)
            {
                shtml.Append(dataPage.ReceptionToolBar);
            }
            return shtml.ToString();
        }

    }
}